/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eleric.rememome.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.eleric.rememome.R;
import com.eleric.rememome.adapter.impl.MemoListAdapter;
import com.eleric.rememome.db.MemoDbAdaptor;
import com.eleric.rememome.db.factory.MemoDbAdaptorFactory;
import com.eleric.rememome.db.factory.impl.MemoDbAdaptorFactoryVer2;
import com.eleric.rememome.memo.Memo;
import com.eleric.rememome.memo.MemoCategory;
import com.eleric.rememome.type.OrderType;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MWaldman
 */
public class ListFrameActivity extends Activity
{

    protected MemoDbAdaptorFactory dbAdapterFactory;

    public ListFrameActivity()
    {
        super();
        dbAdapterFactory = new MemoDbAdaptorFactoryVer2();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        ListView lv;

        lv = (ListView) findViewById(R.id.list_items);
        refreshList(lv);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        final ListView lv;
        final RadioGroup rg;
        final RadioGroup frg;
        RadioButton rb;        
        RadioButton rbDefault;

        MemoDbAdaptor dbAdpator;
        List<MemoCategory> categories;

        setContentView(R.layout.list_frame);
        lv = (ListView) findViewById(R.id.list_items);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

            public void onItemClick(AdapterView<?> av, View v, int position, long id)
            {
//                String item = (String) lv.getItemAtPosition(position);
                Memo item = (Memo) lv.getItemAtPosition(position);
//                Toast.makeText(getApplicationContext(), "Set Properties for: " + item,
//                        Toast.LENGTH_LONG).show();
                Intent editIntent;
                editIntent = new Intent(getApplicationContext(), EditActivity.class);
                editIntent.putExtra(AddActivity.MEMO_REF, item.getId());
//                editIntent.putExtra(AddActivity.MEMO_REF, item);
                startActivity(editIntent);
            }
        });
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {

            public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id)
            {
                deleteWithWarning(position, lv);
                return true;
            }
        });
        rg = (RadioGroup) findViewById(R.id.sort_list_group);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {

            public void onCheckedChanged(RadioGroup arg0, int arg1)
            {
                refreshList(lv);
            }
        });
        frg = (RadioGroup) findViewById(R.id.filter_list_group);
        rbDefault = (RadioButton) findViewById(R.id.all_filter_button);
        dbAdpator = dbAdapterFactory.createDbAdapter(this);
        dbAdpator.open();
        categories = dbAdpator.listCategories();
        for (MemoCategory mc : categories)
        {
            rb = new RadioButton(this);
            rb.setText(mc.getShortName());
            rb.setTextSize(rbDefault.getTextSize());
            rb.setLayoutParams(rbDefault.getLayoutParams());
            frg.addView(rb);
        }
        dbAdpator.close();
        frg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {

            public void onCheckedChanged(RadioGroup arg0, int arg1)
            {
                refreshList(lv);
            }
        });

        refreshList(lv);
    }

    public void resortList(View v)
    {
        ListView lv;

        lv = (ListView) findViewById(R.id.list_items);
        refreshList(lv);
    }

    private void refreshList(ListView lv)
    {
        ListAdapter listAdapter;
//        String[] list;
        Memo[] list;
        OrderType type;
        MemoDbAdaptor dbAdapter;
        RadioGroup rg;
        String newSort;
        String oldSort;
        String alphaSort;
        String prioritySort;
        String checked;
        int checkedId;
        RadioButton rb;
        MemoCategory memoCat;

        rg = (RadioGroup) findViewById(R.id.sort_list_group);
        newSort = getString(R.string.newest_first_order_type);
        oldSort = getString(R.string.oldest_first_order_type);
        alphaSort = getString(R.string.alpha_order_type);
        prioritySort = getString(R.string.priority_order_type);
        checkedId = rg.getCheckedRadioButtonId();
        rb = (RadioButton) findViewById(checkedId);
        checked = (String) rb.getText();

        if (newSort.equals(checked))
        {
            type = OrderType.NEWEST;
        } else if (oldSort.equals(checked))
        {
            type = OrderType.OLDEST;
        } else if (alphaSort.equals(checked))
        {
            type = OrderType.ALPHA;
        } else if (prioritySort.equals(checked))
        {
            type = OrderType.PRIORITY;
        } else
        {
            type = null;
        }
        rg = (RadioGroup) findViewById(R.id.filter_list_group);
        checkedId = rg.getCheckedRadioButtonId();
        rb = (RadioButton) findViewById(checkedId);
        checked = (String) rb.getText();

        dbAdapter = dbAdapterFactory.createDbAdapter(this);
        dbAdapter.open();

        memoCat = dbAdapter.getCategory(checked);
        list = getList(type, memoCat, dbAdapter);
//        listAdapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
//        listAdapter = new ArrayAdapter<Memo>(this, R.layout.list_item, list);
        listAdapter = new MemoListAdapter(this, R.layout.list_item, list);
        lv.setAdapter(listAdapter);
        dbAdapter.close();
    }

    public void filterList(View v)
    {
        ListView lv;

        lv = (ListView) findViewById(R.id.list_items);
        refreshList(lv);
    }

//    private void colorCodeList(ListView lv)
//    {
//        lv.
//    }
    // Look up List in database based on type
//    private String[] getList(OrderType type, MemoDbAdaptor dbAdapter)
    private Memo[] getList(OrderType type, MemoDbAdaptor dbAdapter)
    {
        return getList(type, null, dbAdapter);
    }

    private Memo[] getList(OrderType type, MemoCategory cat, MemoDbAdaptor dbAdapter)
    {
//        String[] list;
        Memo[] list;
        List<String> listObj;
        List<Memo> listMemo;

        listObj = new ArrayList<String>();
//
        if (cat == null)
        {
            listMemo = dbAdapter.listMemos(type);

        } else
        {
            listMemo = dbAdapter.listMemos(type, cat);
        }

//        for (Memo m : listMemo)
//        {
//            listObj.add(m.getMemo());
//        }

//        list = listObj.toArray(new String[0]);
        list = listMemo.toArray(new Memo[0]);

        return list;
    }

    private void deleteWithWarning(int position, ListView lv)
    {
//        final String item;
        final Memo item;
        AlertDialog deleteDialog;
        final ListView lvf = lv;
//        item = (String) getListView().getItemAtPosition(position);
//        item = (String) lv.getItemAtPosition(position);
        item = (Memo) lv.getItemAtPosition(position);

        deleteDialog = new AlertDialog.Builder(this).create();
        deleteDialog.setTitle("Delete Item");
        deleteDialog.setMessage("Are you sure you want to delete: " + item.getMemo());
        deleteDialog.setButton("Yes", new OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                deleteItem(item);
                refreshList(lvf);
            }
        });
        deleteDialog.setButton2("Cancel", new OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // Do Nothing
            }
        });
        deleteDialog.show();

    }

//    private void deleteItem(String item)
    private void deleteItem(Memo item)
    {
        MemoDbAdaptor dbAdapter;
        dbAdapter = dbAdapterFactory.createDbAdapter(this);
        dbAdapter.open();

        dbAdapter.deleteMemo(item.getId());
        Toast.makeText(this, "Deleted: " + item.toString(), Toast.LENGTH_SHORT).show();


        dbAdapter.close();
    }
}
